#include "stdafx.h"
#include "somelib_string.h"


namespace someLib
{
	String::String(void)
	{
	}

	String::String(char value[])
	{
		strcpy(this->value, value);
	}

	String::~String(void)
	{
	}

	int String::GetLength()
	{
		if(this->value)
			return strlen(this->value);
		else return -1;
	}

	char* String::GetValue()
	{
		return this->value;
	}

	char String::GetCharAt(int index)
	{
		if(this->GetLength() > index)
			return *(this->value+index);
		else return NULL;
	}

	void String::ToUpperCase()
	{
		char c;

		for(int i = 0; i < this->GetLength(); i++)
		{
			//check if char at i is a lower case character
			if(this->GetCharAt(i) >= 0x61 && this->GetCharAt(i) <= 0x7A)
			{
				c = this->GetCharAt(i);

				this->value[i] = toupper(c);
			}
		}
	}

	void String::ToLowerCase()
	{
		for(int i = 0; i < this->GetLength(); i++)
		{
			//check if char at i is a lower case character
			if(this->GetCharAt(i) >= 0x41 && this->GetCharAt(i) <= 0x5A)
			{
				this->value[i] += 0x20;
			}
		}
	}

	String* String::operator+(String &b)
	{
		char *result = new char[strlen(this->value)];
		strcpy(result, this->value);
		strcat(result, b.GetValue());
		String *resultObject = new String(result);

		return resultObject;
	}

	bool String::operator==(String &b)
	{
		if(strcmp(this->value, b.GetValue()) == 0) return true;
		else return false;
	}

	bool String::operator>(String &b)
	{
		if(strcmp(this->value, b.GetValue()) > 0) return true;
		else return false;
	}
	bool String::operator<(String &b)
	{
		if(strcmp(this->value, b.GetValue()) < 0) return true;
		else return false;
	}
	bool String::operator>=(String &b)
	{
		if(strcmp(this->value, b.GetValue()) >= 0) return true;
		else return false;
	}
	bool String::operator<=(String &b)
	{
		if(strcmp(this->value, b.GetValue()) <= 0) return true;
		else return false;
	}

	String::operator char*()
	{
		char *result = new char[strlen(this->value)];
		strcpy(result, this->GetValue());
		return result;
	}

	char String::operator [](unsigned index)
	{
		return this->GetCharAt(index);
	}
}